package com.jf.controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/ck")
public class CookieServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie c1 = new Cookie("login","123");
        /*设置cookie的有效期，单位秒*/
        c1.setMaxAge(60*60);
        /*添加cookie的值到浏览器中*/
        response.addCookie(c1);

        /*获取cookie*/
        Cookie[] cookie = request.getCookies();
        for(Cookie c : cookie){
            System.out.println(c.getName());
            System.out.println(c.getValue());
            System.out.println("---");
        }

        for(int i=0;i<cookie.length;i++){
            if(cookie[i].getName().equals("loginId")){
                System.out.println(cookie[i].getName());
                break;
            }
        }
    }
}
